﻿/* 8/26/2015 5:47PM */
var RemoveRecent = RemoveRecent || {};

RemoveRecent = {
    'logging': false
}

RemoveRecent.Log = function (data) {
    if (RemoveRecent.logging) {
        if (typeof console == "undefined") {
            window.console = {
                log: function () { }
            };
        }
        console.log(data);
    }
}

RemoveRecent.CheckNavigation = function() {
    RemoveRecent.Log("RemoveRecent.CheckNavigation");
    
    var clientContext = new SP.ClientContext.get_current();
    var web = clientContext.get_web();
    var quicklaunch = web.get_navigation().get_quickLaunch();

    clientContext.load(quicklaunch);
    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));

    function onQuerySucceeded() {
        var nodeEnumerator = quicklaunch.getEnumerator();
        while (nodeEnumerator.moveNext()) {
            var node = nodeEnumerator.get_current();
            var nodeTitle = node.get_title();
            if (nodeTitle === "Recent") {
                RemoveRecent.DeleteRecentNode(node);
                break;
            }
        }
    }

    function onQueryFailed() {
        RemoveRecent.Log("failed RemoveRecent.CheckNavigation");
    }
}

RemoveRecent.DeleteRecentNode = function (node) {
    RemoveRecent.Log("RemoveRecent.DeleteRecentNode");
    
    var clientContext = new SP.ClientContext.get_current();
    
    node.deleteObject();

    clientContext.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed));
    
    function onQuerySucceeded() {
        RemoveRecent.Log("Removed Recent Node");
        window.parent.location.href = window.location.href;
    }
    
    function onQueryFailed() {
        RemoveRecent.Log("failed RemoveRecent.DeleteRecentNode");
    }
}

SP.SOD.executeOrDelayUntilScriptLoaded(RemoveRecent.CheckNavigation, "sp.js");
